package com.example.shop.goods.dao;

import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.example.shop.goods.dto.GoodsStyle;
import com.example.shop.pojo.GoodsClass;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GoodsStyleDao {
    public static final String ALL_FIELDS_SQL = " gc.goods_class,gc.goods_class_picture,gc.class_price,gc.class_inventory ";

    @Results(id = "GoodsStyleDaoResultMap",value = {
            @Result(column = "goods_class_picture",property = "classPicture"),
            @Result(column = "goods_class",property = "goodsClass"),
            @Result(column = "class_price",property = "classPrice"),
            @Result(column = "class_inventory",property = "classInventory")
    })
    @Select("select " + ALL_FIELDS_SQL +
            "from r_goods_class gc where goods_id = ${goodsId}")
    List<GoodsStyle> selectByGoodsId(Long goodsId);

    @Update("<script>" +
            "update r_goods_class gc join goods g " +
            "on gc.goods_id = g.goods_id " +
            "set " +
            "<if test='ew.goodsClass != null'>" +
                "gc.goods_class_picture = #{ew.goodsClassPicture}," +
            "</if>" +
            "<if test='ew.goodsClass != null'>" +
                "gc.goods_class = #{ew.goodsClass}," +
            "</if>" +
            "<if test='ew.classPrice != null'>" +
                "gc.class_price = ${ew.classPrice}," +
            "</if>" +
            "<if test='ew.classInventory != null'>" +
                "gc.class_inventory = ${ew.classInventory}," +
            "</if>" +
            "g.goods_id = g.goods_id " +
            "where g.merchant_id = ${merchantId} and g.goods_id = ${ew.goodsId} and gc.goods_class = #{beforeClass}" +
            "</script>")
    int updateNotNull(@Param("merchantId")long merchantId,@Param("beforeClass")String beforeClass,@Param(Constants.WRAPPER) GoodsClass style);

}
